import React, { useState } from 'react';
 
const CopyText = ({ text }) => {
  const [copied, setCopied] = useState(false);
 
  const copyToClipboard = (e:any) => {
    e.preventDefault();
    navigator.clipboard.writeText(text).then(() => {
      setCopied(true);
      setTimeout(() => setCopied(false), 2000); // 2秒后重置复制状态
    }).catch(err => {
      console.error('Failed to copy text: ', err);
    });
  };
 
  return (
    <div>
      <span onClick={copyToClipboard} style={{ cursor: 'pointer', textDecoration: 'underline' }}>
        {copied ? '复制成功!' : '一键复制'}
      </span>
      <p>{text}</p>
    </div>
  );
};
 
export default CopyText;